package com.kd.common;

// 正确定义泛型类
public class Result<T> {  // 类级别声明泛型<T>
    private Integer code;
    private String message;
    private T data;       // 使用类定义的泛型T
    private String token;

    // 无参构造
    public Result() {}

    // 全参构造（去掉不安全的强制转换）
    public Result(Integer code, String message, T data, String token) {
        this.code  = code;
        this.message  = message;
        this.data  = data;  // 直接赋值，无需强制转换
        this.token  = token;
    }




    // 静态方法使用泛型（与类泛型独立）
//    public static <T> Result<T> success(T data, String token) {
//        return new Result<>(200, "success", data, token);
//    }



    public static <T> Result<T> success(T data, String message) {
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> success( String message) {
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setMessage(message);
        return result;
    }


    public static <T> Result<T> success(T data, String message,String token) {
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setMessage(message);
        result.setData(data);
        result.setToken(token);
        return result;
    }

    public static <T> Result<T> fail() {
        return new Result<>(201, "账号或密码错误", null, null);
    }

    // 失败静态方法
    public static <T> Result<T> fail(Integer code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> fail( String message) {
        Result<T> result = new Result<>();
        result.setMessage(message);
        return result;

    }


    ///////////////////////////////////////////


    public static class RegisterResult<T> extends Result<T> {
        // 成功方法（保持原有）
        public static <T> RegisterResult<T> success(T data) {
            RegisterResult<T> result = new RegisterResult<>();
            result.setCode(200);
            result.setMessage(" 注册成功");
            result.setData(data);
            return result;
        }

        // 增强的失败方法（新增）
        public static <T> RegisterResult<T> fail(String errorMsg) {
            RegisterResult<T> result = new RegisterResult<>();
            result.setCode(201);
            result.setMessage(errorMsg);
            return result;
        }

        // 链式调用方法（新增）
        public RegisterResult<T> data(T data) {
            this.setData(data);
            return this;
        }

        public RegisterResult<T> message(String message) {
            this.setMessage(message);
            return this;
        }
    }



    // Getter/Setter（保持不变）
    public Integer getCode() { return code; }
    public void setCode(Integer code) { this.code  = code; }
    public String getMessage() { return message; }
    public void setMessage(String message) { this.message  = message; }
    public T getData() { return data; }
    public void setData(T data) { this.data  = data; }
    public String getToken() { return token; }
    public void setToken(String token) { this.token  = token; }
}